package com.controller;


import com.common.utils.Result;
import com.entity.Card;
import com.query.CardQuery;
import com.service.CardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;


/**
 * @author wty
 * @since 2022-01-04
 */
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
@RestController
@RequestMapping("/card")
public class CardController {

    @Autowired
    private CardService cardService;

    /**
     * 添加卡片
     */
    @RequestMapping("/addCard")
    public Result addCard(@RequestBody Card card){
       return cardService.addCard(card);
    }

    /**
     * 修改卡片
     */
    @RequestMapping("/updateCard")
    public Result updateCard(@RequestBody Card card){
        boolean flag = cardService.updateById(card);
        if(flag){
            return  Result.success("修改成功");
        }else{
            return  Result.error("修改失败");
        }
    }

    /**
     * 删除卡片
     */
    @RequestMapping("/delCard/{id}")
    public  Result delCard(@PathVariable("id") Integer id){
        boolean flag = cardService.deleteById(id);
        if(flag){
            return  Result.success("删除成功");
        }else{
            return  Result.error("删除失败");
        }
    }

    /**
     * 根据条件获取卡片
     */
    @RequestMapping("/getCardList")
    public  Result getCardList(@RequestBody CardQuery cardQuery){
       return  cardService.getCardList(cardQuery);
    }

    /**
     * 根据卡片id获取卡片数据
     */
    @RequestMapping("/getCard/{id}")
    public Result getCard(@PathVariable("id") Integer id){
        Card card = cardService.selectById(id);
        return Result.success(card);
    }

    /**
     * 上传图片
     */
    @RequestMapping("/uploadImg")
    public Result uploadImg(MultipartFile file){
        return  cardService.uploadImg(file);
    }


}

